setTimeout(function(){
	$("#name").rules("add",{
		remote: {
			url: 'qz/inters/checkName',
			data:{
				interName:function(){return $("#name").val();}
            },
			type: "get",
			dataType: "json", 
			dataFilter:function(data,type){
				return data=="true"?false:true;
			}
		}, 
		messages: {remote: "该接口已添加"}
	});
} , parseInt(Math.random() * 1000));

var app = angular.module('interApp', []);

$("#interfaceType").select2({
	 placeholder: "请选择接口类型",
	 allowClear: true
});
$("#interfaceType").change(function(){  
	$(this).valid();
}); 

$("#enableStatus").select2({
	 placeholder: "请选择接口部署状态",
	 allowClear: true
});
$("#enableStatus").change(function(){  
	$(this).valid();
}); 

$("#runStatus").select2({
	 placeholder: "请选择接口运行状态",
	 allowClear: true
});
$("#runStatus").change(function(){  
	$(this).valid();
}); 

app.controller('interController', function($scope, $http) {
	var bankMap={};
	var sp_map={};
	$scope.view=function(){
		$http.get('qz/channel/getAll').success(function(response){
			$scope.channelList=response.data;
			$("#channelId").select2({
				 placeholder: "请选择机构",
				 allowClear: true
			});
			$("#channelId").change(function(){  
				$(this).valid();
				$("#businessId").select2("val", "");
				var channelId=$("#channelId").val();
				var url=channelId.length>0?'qz/business/getByChannelId/'+channelId:'qz/business/getAll/';
				$http.get(url).success(function(response){
					$scope.businessList=response.data;
				});
			}); 
        });
			
		$http.get('qz/business/getAll').success(function(response){
			$scope.businessList=response.data;
			$("#businessId").select2({
				 placeholder: "请选择业务分类",
				 allowClear: true
			});
			$("#businessId").change(function(){  
				$(this).valid();
			}); 
        });
			
		$http.get('sys/dic/getByGroup/jym').success(function(response){
			$scope.channelTypeList=response.data;
			$("#channelType").select2({
				 placeholder: "请选择业务细类",
				 allowClear: true
			});
			$("#channelType").change(function(){  
				$(this).valid();
			}); 
        });
			
		$http.get('sys/dic/getByGroup/txxy').success(function(response){
			$scope.protocolList=response.data;
			$("#protocol").select2({
				 placeholder: "请选择通信协议",
				 allowClear: true
			});
			$("#protocol").change(function(){  
				$(this).valid();
			}); 
        });
			
		$http.get('sys/dic/getByGroup/bwgs').success(function(response){
			$scope.packetFormatList=response.data;
			$("#packetFormat").select2({
				 placeholder: "请选择报文格式",
				 allowClear: true
			});
			$("#packetFormat").change(function(){  
				$(this).valid();
			}); 
        });
			
		$http.get('sys/dic/getByGroup/jhfs').success(function(response){
			$scope.idnteractiveList=response.data;
			$("#idnteractive").select2({
				 placeholder: "请选择交互方式",
				 allowClear: true
			});
			$("#idnteractive").change(function(){  
				$(this).valid();
			}); 
        });
		
		$http.get('sys/dic/getByGroup/yhqd').success(function(response){
			$scope.bankList=response.data;
			$("#bankids").select2({
				 placeholder: "请选择支持银行",
				 allowClear: true
			});
			
			$.each($scope.bankList,function(i,bank){
				bankMap[bank.dictionaryId]=bank;
			});
			$scope.bankMap=bankMap;
			
			$("#bankids").change(function(){  
				$('.alert-danger').addClass("display-none");
				/**if($scope.inter_sp.bankids.length>0){
					$('.alert-danger').addClass("display-none");
				}else{
					$('.alert-danger p').html("请选择支持的银行");
				}**/
				for(var key in sp_map){
	  				 if(!$scope.inter_sp.bankids.contains(key)){
	  					 delete sp_map[key];
	  				 }
	  			}
				for(var i=0;i<$scope.inter_sp.bankids.length;i++){
					if(!sp_map[$scope.inter_sp.bankids[i]]){
						sp_map[$scope.inter_sp.bankids[i]]={};
					}
				}
			}); 
        });
		
		$http.get('sys/dic/getByGroup/zfklx').success(function(response){
			$scope.zfklxList=response.data; 
        });
		
		$http.get('sys/dic/getByGroup/zfksx').success(function(response){
			$scope.zfksxList=response.data; 
        });
	}
	
	$scope.FileUpload=function(file) {
		console.log(file);
	}
	
	$scope.selZfksx=function(idStr){
		var ids=idStr.split("-");
		if(ids.length==3){
			var bankid=ids[0];
			var zfklx=ids[1];
			var zfksx=ids[2];
			if(sp_map[bankid] == undefined){
				sp_map[bankid]={};
			}
			if(sp_map[bankid][zfklx] == undefined){
				sp_map[bankid][zfklx]={};
			}
			if(sp_map[bankid][zfklx][zfksx] == undefined){
				sp_map[bankid][zfklx][zfksx]=0;
			}else{
				delete sp_map[bankid][zfklx][zfksx];
			}
			if(Object.keys(sp_map[bankid][zfklx]).length==0){
				$("#icon_"+bankid+"_"+zfklx).removeClass("fa fa-check-circle");
				delete sp_map[bankid][zfklx];
			}else{
				$("#icon_"+bankid+"_"+zfklx).addClass("fa fa-check-circle");
			}
			$('.alert-danger').addClass("display-none");
		}
	}
	
	$scope.refreshData=function(){
		$scope.view();
	}
	
	$scope.interSave=function(){
	   if($("#myForm").valid()){
		   $http.post('qz/inters/add/', $scope.inter).success(function(){
               redirectUrl("manage/inter/","list.jsp");
		   });
	   }
	}
	
	$scope.interReset=function(){
		$scope.view();
	}
	
	$scope.interCancle=function(){
		redirectUrl("manage/inter/","list.jsp");
	}
	
	$scope.initFormWizard=function(){
		if (!jQuery().bootstrapWizard) {
            return;
        }
        
		function forward(tab, navigation, index){
			$('.alert-danger').addClass("display-none");
		   var total = navigation.find('li').length;
   		   var current = index + 1;
//   		   $('.stepHeader', $('#formWizard')).text('Step ' + (index + 1) + ' of ' + total);
   		   jQuery('li', $('#formWizard')).removeClass("done");
   		   var li_list = navigation.find('li');
   		   for (var i = 0; i < index; i++) {
   			   jQuery(li_list[i]).addClass("done");
   		   }
   		   if (current == 1) {
   			   $('#formWizard').find('.prevBtn').hide();
   		   } else {
   			   $('#formWizard').find('.prevBtn').show();
   		   }
   		   if (current >= total) {
                $('#formWizard').find('.nextBtn').hide();
                $('#formWizard').find('.submitBtn').show();
   		   } else {
                $('#formWizard').find('.nextBtn').show();
                $('#formWizard').find('.submitBtn').hide();
   		   }
		}
		var interId="";
        /*-----------------------------------------------------------------------------------*/
		/*	Initialize Bootstrap Wizard
		/*-----------------------------------------------------------------------------------*/
        $('#formWizard').bootstrapWizard({
            'nextSelector': '.nextBtn',
            'previousSelector': '.prevBtn',
             onNext: function (tab, navigation, index) {
            	 if(index==1){
                 	if($("#myForm").valid()){
                 		if(interId==""){
                 			$http.post('qz/inters/add/', $scope.inter).success(function(response){
     	           			   forward(tab, navigation, index);
     	           			   interId=response.data
     	                   });
                 		}else{
                 		   $scope.inter.channelInterId=interId;
                 		   $http.put('qz/inters/modify/', $scope.inter).success(function(response){
       	           			   forward(tab, navigation, index);
       	                   });
                 		}
	           	 	}else{
	           	 		return false;
	           	 	}
            	 }else if(index==2){
            		 var errorMes="";
            		 if($scope.inter_sp != undefined && $scope.inter_sp.bankids.length>0){
            			 $.each($scope.inter_sp.bankids,function(i,bankid){
            				 var errMes="";
                			 if(sp_map[bankid] == undefined){
                				 errMes="未进行设置 ";
                			 }else{
                				 var bcode=$("#"+bankid+"_bcode").val();
                				 if(bcode.length==0){
                					 errMes="未设置机构银行代码、"
                					 delete sp_map[bankid].code;
                				 }else{
                					 sp_map[bankid].code=bcode;
                				 }
                				 if((sp_map[bankid].code==undefined&&Object.keys(sp_map[bankid]).length==0)
                						 ||(sp_map[bankid].code!=undefined&&Object.keys(sp_map[bankid]).length==1)){
                					 errMes+="未设置支持的卡类型、";
                				 }else{
                					 $.each($scope.zfklxList,function(i,zfklx){
                    					 if(sp_map[bankid][zfklx.dictionaryId]!=undefined){
                    						 var zfklxCode=$("#"+bankid+"_"+zfklx.dictionaryId+"_zfklx").val();
                            				 if(zfklxCode.length==0){
                            					 errMes+="未设置"+zfklx.name+"代码、";
                            					 delete sp_map[bankid][zfklx.dictionaryId].code;
                            				 }else{
                            					 sp_map[bankid][zfklx.dictionaryId].code=zfklxCode;
                            				 }
                            				 sp_map[bankid][zfklx.dictionaryId].id=zfklx.dictionaryId;
                            				 var zfksx=false;
                            				 for(var key in sp_map[bankid][zfklx.dictionaryId]){
                            					 if(key != "id" && key != "code"){
                            						 zfksx=true;
                            						 break;
                            					 }
                            				 }
                            				 if(!zfksx){
                            					 errMes+="未设置"+zfklx.name+"属性、";
                            				 }
                    					 }
                    				 });
                				 }
                			 }
                			 if(errMes.length>0){
                				 errorMes+=bankMap[bankid].name+errMes.substring(0,errMes.length-1)+"<br>";
                			 }
                		 });
            		 }/**else{
            			 errorMes+="请选择支持的银行";
            		 }**/
            		 if(errorMes.length>0){
            			 $('.alert-danger').removeClass("display-none");
            			 $('.alert-danger p').html(errorMes);
            			 return false;
            		 }else{
            			 $('.alert-danger').addClass("display-none");
            			 for(var key in sp_map){
            				 if(!$scope.inter_sp.bankids.contains(key)){
            					 delete sp_map[key];
            				 }
            			 }
            			 var param={
            					 interId:interId,
            					 channelId:$scope.inter.channelId,
            					 spJson:JSON.stringify(sp_map)
            			 }
            			 $http.put('qz/inters/modifySpBank/', param).success(function(){
 	            		 	forward(tab, navigation, index)
             			 });
            		 }
            	 }else{
            		 
            	 }
            },
            onPrevious: function (tab, navigation, index) {
            	forward(tab, navigation, index);
            },
			onTabClick: function (tab, navigation, index) {
//                bootbox.alert('On Tab click is disabled');
                return false;
            },
            onTabShow: function (tab, navigation, index) {
                var total = navigation.find('li').length;
                var current = index + 1;
                var $percent = (current / total) * 100;
                $('#formWizard').find('.progress-bar').css({
                    width: $percent + '%'
                });
            }
        });

        $('#formWizard').find('.prevBtn').hide();
        $('#formWizard .submitBtn').click(function () {
        	var deployInfo=$scope.deployInfo; 
 	   		if(deployInfo!=undefined && deployInfo.length>0){
 	   		  var regDeploy = /^([0|1],((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))),([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]),(\w|-|\.|\/){0,125},(\w|-|\.|\/){0,125},(\w|-|\.|\/){0,125}\n)+$/;
 	   		  if(regDeploy.test(deployInfo+"\n")){
     	   			var param={
    	   				interId:interId,
    	   				deployInfo:deployInfo
    	   			 }
        			$http.put('qz/inters/modifyDeploy/', param).success(function(response){
     	   				 if(response.meta.success){
     	   					redirectUrl("manage/inter/","list.jsp");
     	   				 }
     	   			});
        			$('.alert-danger').addClass("display-none");
        		}else{
        			$('.alert-danger').removeClass("display-none");
	   					$('.alert-danger p').html("请检查服务器配置信息格式");
	   					return false;
        		}
 	   		 }else{
 	   			$('.alert-danger').removeClass("display-none");
 	   			$('.alert-danger p').html("请输入服务器配置信息");
 	   			return false;
 	   		 }
        }).hide();
	}
	
	$scope.view();
	$scope.initFormWizard();
});

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}



